﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMS.IRepository;


namespace CMS.Repository
{
    /// <summary>
    /// CourseRepository implement from ICourseRepository
    /// Author:Fiona Pan
    /// </summary>
    public class CourseRepository : ICourseRepository
    {
        /// <summary>
        /// Select all courses from database.
        /// </summary>
        /// <returns>List of Course.</returns>
        public List<CMS.Model.Course> SelectAllCourses(int pageIndex, int pageSize, out int rowCount)
        {
            List<Course> courseEFList = new List<Course>();
            using (var cmsContext = new CMSEntities())
            {
                var courses = from course in cmsContext.Courses.Include("Level").Include("Teacher")
                              select course;
                rowCount = courses.Count();
                courses = courses.OrderBy(course => course.CourseId).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                courseEFList = courses.ToList();
            }

            return Translator.CourseTranslator.ToCourseModelList(courseEFList);
        }
        /// <summary>
        /// Select courses from database by conditions.
        /// </summary>
        /// <param name="courseName">string courseName</param>
        /// <param name="courseStatus">bool courseStatus</param>
        /// <returns></returns>
        public List<CMS.Model.Course> SelectCoursesByCondition(string courseName, string courseStatus, int pageIndex, int pageSize, out int rowCount)
        {
            List<Course> courseEFList = new List<Course>();
            using (var cmsContext = new CMSEntities())
            {
                var courses = cmsContext.Courses.Include("Teacher").Include("Level").AsQueryable();
                if (!string.IsNullOrEmpty(courseStatus))
                {
                    bool courseStatusFlag = bool.Parse(courseStatus);
                    courses = courses.Where(c => c.CourseStatus == courseStatusFlag);
                }
                if (courseName != null)
                {
                    courses = courses.Where(c => c.CourseName.Contains(courseName));
                }
                rowCount = courses.Count();
                courses = courses.OrderBy(c => c.CourseId).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                courseEFList = courses.ToList();

            }
            return Translator.CourseTranslator.ToCourseModelList(courseEFList);
        }


        /// <summary>
        /// Delete course in database.
        /// </summary>
        /// <param name="courseId">int courseId</param>
        public void DeleteCourseById(int courseId)
        {
            using (var cmsContext = new CMSEntities())
            {
                var courseDetails = from cd in cmsContext.CourseDetails
                                    where cd.CourseId == courseId
                                    select cd;
                var course = (from c in cmsContext.Courses
                              where c.CourseId == courseId
                              select c).First();
                foreach (CourseDetail cds in courseDetails)
                {
                    cmsContext.CourseDetails.DeleteObject(cds);
                }
                cmsContext.Courses.DeleteObject(course);
                cmsContext.SaveChanges();
            }
        }
        /// <summary>
        /// Update CourseStatus.
        /// </summary>
        /// <param name="course">The course.</param>
        public void UpdateCourseStatus(CMS.Model.Course course)
        {
            using (var cmsContext = new CMSEntities())
            {
                Course updateCourse = cmsContext.Courses.SingleOrDefault(c => c.CourseId == course.CourseId);
                if (updateCourse != null)
                {
                    updateCourse.CourseStatus = false;
                    cmsContext.SaveChanges();
                }
            }
        }
        /// <summary>
        /// Inserts the course.
        /// </summary>
        /// <param name="course">The course.</param>
        /// <returns></returns>
        public int InsertCourse(CMS.Model.Course course)
        {

            using (var cmsContext = new CMSEntities())
            {
                Course _course = Translator.CourseTranslator.ToCourseEF(course);
                cmsContext.Courses.AddObject(_course);
                cmsContext.SaveChanges();
                return _course.CourseId;
            }
        }
        /// <summary>
        /// Updates the course.
        /// </summary>
        /// <param name="course">The course.</param>
        public void UpdateCourse(CMS.Model.Course course)
        {
            using (var cmsContext = new CMSEntities())
            {
                Course _course = cmsContext.Courses.SingleOrDefault(c => c.CourseId == course.CourseId);
                _course.CourseId = course.CourseId;
                _course.CourseName = course.CourseName;
                _course.CourseStatus = course.CourseStatus;
                _course.LevelId = course.LevelId;
                _course.TeacherId = course.TeacherId;
                cmsContext.SaveChanges();

            }
        }
        /// <summary>
        /// Selects the name of the course.
        /// </summary>
        /// <param name="courseId">The course id.</param>
        /// <returns></returns>
        public List<Model.Course> SelectCourseName(int courseId)
        {
            using (var cmsContext = new CMSEntities())
            {
                var courses = (from c in cmsContext.Courses
                               where c.CourseId == courseId
                               select c).ToList<Course>();

                return Translator.CourseTranslator.ToCourseModelList(courses);
            }
        }


        /// <summary>
        /// Gets the courses.
        /// </summary>
        /// <param name="PageIndex">Index of the page.</param>
        /// <param name="PageSize">Size of the page.</param>
        /// <returns></returns>
        public List<Model.Course> GetCourses(int PageIndex, int PageSize)
        {
            int pageIndex = PageIndex;
            List<Course> courseEFList = new List<Course>();
            using (var cmsContext = new CMSEntities())
            {
                var courses = (from course in cmsContext.Courses.Include("Level").Include("Teacher")
                               select course).OrderBy(course => course.CourseId).Skip(pageIndex * PageSize).Take(PageSize);
                courseEFList = courses.ToList();
            }
            return Translator.CourseTranslator.ToCourseModelList(courseEFList);
        }
    }
}
